require('../css/user.scss')
define(['../userModule'], function(module) {
	module.controller("userListCtrl", ['$scope','$rootScope','httpService','$timeout','ENV','growl',
		function($scope,$rootScope,httpService,$timeout,ENV,growl) {
		'use strict';
		// 查询对象
		$scope.searchData = {
			key: 'mobileNo',
			value: '',
		};
		// 表格对象
		$scope.tableConf = {
			"tableHead": [{
				"name": "序号",
				"width": "1"
			}, {
				"name": "客户号",
				"width": "2"
			}, {
				"name": "账号",
				"width": "1"
			}, {
				"name": "联系方式",
				"width": "2"
			}, {
				"name": "手机",
				"width": "2"
			}, {
				"name": "姓名",
				"width": "1"
			}, {
				"name": "性别",
				"width": "1"
			}, {
				"name": "操作",
				"width": "2"
			}],
			"tableTr": []
		};

		// 分页配置
		$scope.conf = {
			currentPage: 1,
			total: 0,
			itemPageLimit: 20,
			// 是否显示一页选择多少条
			isSelectPage: true,
			// 是否显示快速跳转
			isLinkPage: false
		};
		$scope.delUser = function (id) {
			httpService.linkHttp({
				url:ENV.userDelete
			},{userId: id}).then(function (res) {
                // 判断请求成功
                var result = res.data;
                if (result.code == "000000") {
                    list();
				}
            })
        };

		// 赋值
		function assignFn(data) {
			// 表格数据
			$scope.tableConf.tableTr = data.userList;
			// 分页数据
			$scope.conf.total = parseInt(data.totalRow); //总条数
			$scope.conf.currentPage = parseInt(data.pageNum);
			$scope.conf.itemPageLimit = parseInt(data.pageSize);
		}
		//清除
		function resetTable() {
			$scope.tableConf.tableTr = [];
			$scope.conf.total = 0;
			$scope.conf.currentPage = 1;
		}

		// 分页请求
		$scope.$watch('conf.currentPage', function(newV, oldV) {
			if (newV != oldV && oldV != 0 && newV != 0) {
				var data = {};
				if ($scope.searchData.key) data[$scope.searchData.key] = $scope.searchData.value;
                if ($scope.searchData.value && !$scope.searchData.key)  {
                    growl.addErrorMessage("请选择查询关键词");
                    return;
                }
				data['pageNum'] = newV;
				httpService.linkHttp({
					url: ENV.userList
				}, data).then(function(res) {
					// 判断请求成功
					var result = res.data;
					if (result.code == "000000" && result.data) {
						assignFn(result.data);
					}else {
                        resetTable();
                        var errText;
                        result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                        growl.addErrorMessage(errText);
					}
				});
			}
		});

		// 首次加载
		var list = function () {
            httpService.linkHttp({
                url: ENV.userList,
                isErrMsgFn: function() {}
            }).then(function(res) {
                // 判断请求成功
                var result = res.data;
                if (result.code == "000000" && result.data) {
                    assignFn(result.data);
                }else {
                    resetTable();
                    var errText;
                    result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                    growl.addErrorMessage(errText);
                }
            });
        };
		list();

		// 查询操作
		$scope.userSearch = function() {
			var data = {};
			if ($scope.searchData.key) data[$scope.searchData.key] = $scope.searchData.value;
			if ($scope.searchData.value && !$scope.searchData.key)  {
				growl.addErrorMessage("请选择查询关键词");
				return;
			}
            httpService.linkHttp({
				url: ENV.userList
			}, data).then(function(res) {
				var result = res.data;
				if (result.code == "000000" && result.data) {
					assignFn(result.data);
				} else {
                    resetTable();
                    var errText;
                    result.errDesc ? errText = result.errDesc : errText = '请求获取失败, 请稍后再尝试';
                    growl.addErrorMessage(errText);
				}

			})
		}
		

	}])
});
